<template>
	<view>
		<view class="house-list">
			<button @click="addData">新增</button>
			<view class="house-item" v-for="item in houseList">
				{{item.title}}
				<button size="mini" @click="deleteHouse(item._id)">删除</button>
				<button size="mini" @click="editHouse(item._id)">编辑</button>
			</view>
		</view>
	</view>
</template>

<script setup>
import { reactive, ref } from 'vue';
	const pageInfo = reactive({
		pageSize: 10,
		pageNum: 1,
		total: 0
	})
	const houseList = ref([])
	// 调用创建的云函数，获取数据
	const getData = async () => {
		const res = await uniCloud.callFunction({
			name:'getHouseList',
			data: {
				pageSize: pageInfo.pageSize,
				pageNum: pageInfo.pageNum
			}
		})
		houseList.value = res.result.data.list
	}
	getData()
	const addData = () => {
		const params = {
			
			areaNum: 10000 * Math.random() + 10000,
			
			houseType: "五室两厅",
			
			imgUrl: "http://gips3.baidu.com/it/u=1821127123,1149655687&fm=3028&app=3028&f=JPEG&fmt=auto?w=720&h=1280",
			
			localName: "天通苑二区",
			
			localType: "小区",
			
			title:"石景山 碧桂园 紧邻高铁、地铁",
			
			unitPrice: 20000 * Math.random() + 30000,
			
			isLook: false,
			a: 1
		}
		uniCloud.callFunction({
			name:'addHouse',
			data: params,
		}).then(res => {
			console.log(res)
			if(res.result.id) {
				getData()
			}
		})
	}
	const deleteHouse = (id) => {
		uniCloud.callFunction({
			name:'deleteHouse',
			data:{
				id
			}
		}).then(res => {
			uni.showToast({
				title:'删除成功'
			})
			getData()
		})
	}
	const editHouse = async (id) => {
		const res= await uniCloud.callFunction({
			name:'editHouse',
			data:{
				id,
				title: '这是修改之后的title' + Date.now()
			}
		})
		console.log(res)
		getData()
	}
	// uniCloud.callFunction({
	// 	name:'getHouseList',
	// 	data: {}
	// }).then(res => {
	// 	console.log(res)
	// })
	// uniCloud.callFunction({
	// 	name:'getHouseList',
	// 	data: {},
	// 	success(res) {
	// 		console.log(res)
	// 	}
	// })
</script>

<style>
	       
</style>
